package com.dongchedi.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongchedi.entity.LoginTicket;
import com.dongchedi.service.FindLoginService;
import com.dongchedi.utils.ResultMessage;
import com.dongchedi.utils.constant.RedisConstantKey;
import com.dongchedi.utils.constant.TimeConstant;
import com.dongchedi.vo.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
 * @author leibidong
 * @description   用户登录注销controller层
 * @date 2021/6/24
 */
@Controller
@RequestMapping("api")
public class LoginController {
    @Autowired
    FindLoginService findLoginService;
    @Resource
    RedisTemplate<String,Object> redisTemplate;
    /*
     * @Author: leibidong
     * @Description: 用户登录
     * @Date: 2021/6/26 20:14
     * @Param: [loginVO]
     * @Return: java.lang.String
     **/
   @PostMapping("/login")
   @ResponseBody
    public String login(LoginVO loginVO , HttpServletResponse httpServletResponse){
      Map<String,Integer> map=findLoginService.login(loginVO);
      if(map.get("id").equals(0)){
          Map<String,Object> map1=new HashMap<>();
          map1.put("id",map.get("id"));
          String s= ResultMessage.getJSONString(0,"登录失败",map1);
          return s;
      }else  if(map.get("id").equals(-1)){
          JSONObject jsonObject=new JSONObject();
          return ResultMessage.getJSONObjectString(0,"密码或账号不符合要求",jsonObject);
      }
      else {
          String uuid=ResultMessage.generateUUID();
          String loginRedisKey= RedisConstantKey.getLoginRedisKey(uuid);
          LoginTicket loginTicket = new LoginTicket();
          loginTicket.setUserId(map.get("id"));
          loginTicket.setStatus(1);
          loginTicket.setTicket(uuid);
          loginTicket.setExpired(new Date(System.currentTimeMillis() + TimeConstant.DEFAULT_EXPIRED_SECONDS.getTimes()* 1000));
          redisTemplate.opsForValue().set(loginRedisKey,loginTicket);
          Cookie cookie=new Cookie("ticket",uuid);
          httpServletResponse.setHeader("Access-Control-Allow-Credentials", "true");
          httpServletResponse.addCookie(cookie);
          Map<String,Object> map1=new HashMap<>();
          map1.put("id",map.get("id"));
          String s= ResultMessage.getJSONString(1,"登录成功",map1);
          return s;
      }
    }
    /*
     * @Author: leibidong
     * @Description: 注销用户登录
     * @Date: 2021/6/26 20:14
     * @Param: [id]
     * @Return: java.lang.String
     **/
    @GetMapping("/logout")
    @ResponseBody
    public String loginout( Integer id,@CookieValue("ticket") String ticket){
      Integer integer= findLoginService.loginout(id,ticket);
        JSONObject jsonObject=new JSONObject();
      if(integer!=0) {
          jsonObject.put("status","ok");
         return  ResultMessage.getJSONObjectString(1,"注销成功",jsonObject);
      }
        return  ResultMessage.getJSONObjectString(0,"注销失败",jsonObject);
    }
}
